import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    secretKey: "hxov",   //crypto-js的密钥
    hasLogin: false,
    token: "flc",
    opts: [],    //页面构建的基本数据options
    dicts: [],   //页面展示需要的字典翻译
    brands: [],  //手机品牌信息
    // address: [],   //中国三级行政数据
    userInfo: {
      avatar: "/img/avatar.gif",
      name: "用户姓名",
      nickname: "用户昵称",
      waitPay: 0,
      waitSend: 0,
      waitGet: 0,
      waitComment: 0,
    },    //登录之后的用户信息
  },
  mutations: {
    // changeAvatarSrc(state, data) {
    //   state.avatarSrc = data;
    // },
    // 封装函数统一处理mutations
    //示例：使用vuex.commit('save', {path:'a.b.c', data:val})，结果state.a.b.c = '我是需要保存的数据'
    save(state, { path, data }) {
      if (!path) {
        throw new Error('need path')
      }
      let keyPath = path.split('.')
      let needSave = state
      for (let i = 0; i < keyPath.length - 1; i++) {
        needSave = needSave[keyPath[i]]
        if (!needSave) {
          throw new Error(`error path: ${keyPath[i]}`)
        }
      }
      needSave[keyPath[keyPath.length - 1]] = data
    },

  },
  actions: {},
  modules: {}
});
